<?php

namespace App\Http\Controllers\Admin;

use App\Http\Models\Product;
use App\Http\Models\Category;

class ProductController extends CommonController{
   
    public function index(){
        
        $cate_product = Product::join('category', 'category.cate_id', '=', 'product.product_cate_id')
                      ->orderBy('product_createtime', 'desc')->paginate(10);
        return view('admin.product.index', ['cate_product' => $cate_product]);
    }
    
    public function create(){
        $cate = Category::tree();
        return view('admin.product.add', ['cate' => $cate]);
    }    
    
    public function store(){
        $input = \Input::except('_token');
        if ($input) {
            $rules = [
                'product_name' => 'required',
                'product_thumb' => 'required',
                'product_cate_id' => 'required'
            ];

            $messages = [
                'product_name.required' => '产品名称不能为空！',
                'product_thumb.required' => '产品图片不能为空！',
                'product_cate_id.required' => '产品分类不能为空！'
            ];
            
            $validator = \Validator::make($input, $rules, $messages);
            if ($validator->passes()) {
                $input['product_createtime'] = time();
                $input['product_updatetime'] = time();
                
                $r = Product::create($input);
              
                if($r)
                    return redirect ('admin/product');
                else 
                    return back()->with ('errors', '创建失败，请稍后再试！');
            } else
                return back()->withErrors($validator);
        }
    }

    public function show(){
        
    }

    public function edit($product_id){
        $data = Product::find($product_id);
        return view('admin.product.edit', ['data' => $data]);
    } 
    
    public function update($product_id){
        $input = \Input::except('_token', '_method');
        
        $rules = [
                'product_email' => 'email',
                'old_product_password' => 'required',
                'product_password' => 'required|min:6|confirmed'
            ];

        $messages = [
                'product_email.email' => '产品邮箱格式不正确！',
                'product_password.required' => '产品密码不能为空！',
                'old_product_password.required' => '产品原密码不能为空！',
                'product_password.min' => '产品密码太短，不到6位！',
                'product_password.confirmed' => '确认密码与密码不匹配'
            ];

        $validator = \Validator::make($input, $rules, $messages);
        $product = Product::find($product_id);
        $oldpassword = md5($input['old_product_password']);
        $validator->after(function($validator) use ($oldpassword, $product) {
            if ($product->product_password != $oldpassword) {
                $validator->errors()->add('errors', '原密码错误');
        }});
        
        if ($validator->fails()) 
            return back()->withErrors($validator);
        
        $submit['product_email'] = $input['product_email'];
        $submit['product_password'] = md5($input['product_password']);
        $submit['product_updatetime'] = time();
        $r = $product->update($submit);
        
        return back()->with('errors', ($r) ? '产品更新成功':'产品更新失败，请稍后再试！');
    }

    public function destroy($product_id){
        $r = Product::where('product_id', $product_id)->delete();
        
        $data = [];
      
        $data['status'] = ($r) ? 0 : 1;
        $data['msg'] = ($r) ? '删除成功':'删除失败请稍后再试！';

        return $data;  
    }
    
}
